using System;
using System.Collections;
using UnityEngine;

/*
* Introduction：
* Creator：
*/

namespace HyxFrame.Core
{

    /// <summary>
    /// 
    /// </summary>
    public class AudioManager : MonoSingleton<AudioManager>
    {


        [SerializeField] private AudioSource soundEffectSource;
        [SerializeField] private AudioSource musicSource;


        // 新增的音频播放完成回调
        private Action OnSoundEffectComplete;
        private Action OnMusicComplete;

        private Action OnMusicCompleteDefluat;


        // Start is called before the first frame update
        void Start()
        {

        }

        protected override void InitManager()
        {

        }



        private void CreateObj()
        {
            musicSource = new GameObject("musicSource").AddComponent<AudioSource>();
            musicSource.transform.parent = transform;

            soundEffectSource = new GameObject("soundEffectSource").AddComponent<AudioSource>();
            soundEffectSource.transform.parent = transform;

            // 注册音频播放完成回调
            //OnSoundEffectComplete += () => Debug.Log("SoundEffect Complete");
            //OnMusicComplete += () => Debug.Log("Music Complete");
        }



        public void AddNewAudioSource(AudioSource audioSource)
        {

        }



        #region 设置播放器
        /// <summary>
        /// 播放器，控制无事件的默认播放回调
        /// </summary>
        /// <param name="clip"></param>
        /// <param name="action"></param>
        public void PlayBGMusicDefluatAction(AudioClip clip, Action action = null)
        {
            if (clip != null && musicSource != null)
            {
                musicSource.clip = clip;
                musicSource.Play();
                OnMusicCompleteDefluat = action;

                StartCoroutine(WaitForAudioFinish());
            }
        }

        /// <summary>
        /// 设置音量
        /// </summary>
        /// <param name="volume"></param>
        public void SetMusicVolume(float volume)
        {
            if (musicSource != null)
            {
                musicSource.volume = Mathf.Clamp01(volume);
            }
        }

        /// <summary>
        /// 设置音乐
        /// </summary>
        /// <param name="clip"></param>
        /// <param name="action"></param>
        public void PlayMusic(AudioClip clip, Action action = null)
        {
            if (clip != null && musicSource != null)
            {
                musicSource.clip = clip;
                musicSource.Play();
                OnMusicComplete = action;

                StartCoroutine(WaitForAudioFinish());
            }
        }

        private IEnumerator WaitForAudioFinish()
        {
            yield return new WaitForSeconds(musicSource.clip.length);
            if (OnMusicComplete != null)
            {
                OnMusicComplete.Invoke();
                OnMusicComplete = null; // 清空回调
            }
            else
            {
                OnMusicCompleteDefluat?.Invoke();
            }
        }

        public void StopMusic()
        {
            if (musicSource != null && musicSource.isPlaying)
            {
                musicSource.Stop();
                musicSource.clip = null;
                if (OnMusicComplete != null)
                {
                    OnMusicComplete = null;
                }
            }
        }

        #endregion

        #region 其他音效播放器


        public void PlaySoundEffect(AudioClip clip, Action action)
        {
            if (clip != null && soundEffectSource != null)
            {
                soundEffectSource.clip = clip;
                soundEffectSource.Play();
                OnSoundEffectComplete = action;
            }
        }
        public void SetSoundEffectVolume(float volume)
        {
            if (soundEffectSource != null)
            {
                soundEffectSource.volume = Mathf.Clamp01(volume);
            }
        }



        public void StopSoundEffect()
        {
            if (soundEffectSource != null && soundEffectSource.isPlaying)
            {
                soundEffectSource.Stop();
                soundEffectSource.clip = null;
                // 触发音频播放完成事件
            }
        }

        #endregion

    }
}